<?php

namespace App\Http\Controllers\AdminApi;

use App\Model\AdminTable;
use App\Model\ClassTable;
use App\Model\FieldTable;
use App\Model\PriceSetTable;
use App\Model\PriceSetTempTable;
use App\Model\StorefrontTable;
use App\Repository\BaseController\ApiController;
use App\Repository\Common\ConstTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;

//店面
class StorefrontController extends ApiController
{

    /**
     * @api {get} adminapi/storefront/list 店面列表
     * @apiGroup Admin-Store
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} page --
     * @apiParam {Int} page_num --
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "count": "2",
     * "list": [
     * {
     * "id": "2",
     * "store_name": "武昌",
     * "manager_name": "陈",
     * "phone": "3424252525",
     * "field_count": ""
     * },
     * {
     * "id": "1",
     * "store_name": "汉阳",
     * "manager_name": "王",
     * "phone": "1231412414",
     * "field_count": ""
     * }
     * ],
     * "page": 1
     * }
     * }
     */
    public function getList()
    {
        $page = \Request::get('page', 1);
        $limit = \Request::get('page_num', 10);
        $key = \Request::get('key');
        return JsonRespose::done(StorefrontTable::getList($page, $limit, $key));
    }

    /**
     * @api {get} /adminapi/storefront/fields 获取店面下所有场地类型
     * @apiGroup Admin-Store
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} store_id  店铺id
     *
     * @apiSuccessExample {json} Success-Response:
     *
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": [
     * "1" // 表示几V几   5就是5V5
     * ]
     * }
     */
    public function getFields()
    {
        $store_id = \Request::get('store_id');  //店面
        $where = 'storefront_id =:storefront_id';
        $param['storefront_id'] = $store_id;
        $res = \DB::select("select * from field WHERE {$where} GROUP BY type", $param);
        $list = [];
        foreach ($res as $v) {
            $v->type_text = Str::getTypeText($v->type);
            $v->type = intval($v->type);
            $list[] = $v;
        }
        return JsonRespose::success(['list' => $list]);
    }


    /**
     * @api {get} adminapi/storefront/info 店面详情
     * @apiGroup Admin-Store
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} id --
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "id": "2",
     * "store_name": "武昌",
     * "manager_name": "陈",
     * "phone": "3424252525",
     * "field_count": ""
     * }
     * }
     */
    public function getInfo()
    {
        $id = \Request::get('id');
        StorefrontTable::updateCountField($id);
        $res = StorefrontTable::ORM()->where('id', $id)->first();
        return JsonRespose::success($res);
    }

    /**
     * @api {get} adminapi/storefront/save 修改保存店面
     * @apiGroup Admin-Store
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} store_name 店铺名称
     * @apiParam {Int} manager_name 店长
     * @apiParam {Int} phone 手机号
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * "id": 3
     * }
     * }
     */
    public function postSave()
    {
        $id = \Request::get('id');
        $store_name = \Request::get('store_name');
        $store_adds = \Request::get('store_adds');
        $manager_name = \Request::get('manager_name');
        $img = \Request::get('img');
        $phone = \Request::get('phone');

        StorefrontTable::updateCountField($id);

        if ($id) {
            $res = StorefrontTable::ORM()->where('id', $id)->update([
                'store_name' => $store_name,
                'manager_name' => $manager_name,
                'img' => $img,
                'phone' => $phone,
                'store_adds' => $store_adds,
            ]);

        } else {
            $res = StorefrontTable::ORM()->insertGetId([
                'store_name' => $store_name,
                'manager_name' => $manager_name,
                'img' => $img,
                'phone' => $phone,
                'store_adds' => $store_adds
            ]);
        }
        StorefrontTable::updateCountField($id);
        if ($res) {
            return JsonRespose::success(['id' => $res]);
        } else {
            return JsonRespose::fail();
        }
    }

    /**
     * @api {post} adminapi/storefront/del 删除店面
     * @apiGroup Admin-Store
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} id -
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     * }
     * }
     */
    public function postDel()
    {
        $id = \Request::get('id');
        StorefrontTable::ORM()->where('id', $id)->delete();
        FieldTable::ORM()->where('storefront_id', $id)->delete();
        PriceSetTable::ORM()->where('storefront_id', $id)->delete();
        PriceSetTempTable::ORM()->where('storefront_id', $id)->delete();

        return JsonRespose::success();
    }
}